package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.FuturesAccountProcessor;
import tech.abing.spider.task.pageprocessor.amac.OutsourceOrganizationProcess;
import tech.abing.spider.task.pipeline.amac.OutsourceOrganizationPipeline;
import tech.abing.spider.util.SqlUtil;

public class OutsourceOrganizationSpider {

	private static SqlUtil sqlUtil = null;

	
	public static void start(){//start()
		
		CrawlManager manager = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = manager
		.prepareCrawlTask(DictEnum.TASK_TYPE_OUTSOURCE_ORGANIZATION.getName(), DefaultPageDownloader.class)
		.useThread(2)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(200)
		.usePipeline(OutsourceOrganizationPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_OUTSOURCE_ORGANIZATION.getCode());
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		manager.start(task);
	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			String url = "http://fo.amac.org.cn/amac/allNotice.do";
			
			for(int i = 2;i<=4;i++){//2:份额登记业务外包服务;3:估值核算业务外包服务,4:信息技术系统外包服务
				
				PageRequest req = context.createPageRequest(url, OutsourceOrganizationProcess.class);
				
				req.setMethod(Method.POST);
				req.setPostType(PostType.FORMDATA);
				
				req.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
				req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
				req.putHeader("Accept-Encoding", "gzip, deflate");
				req.putHeader("Cache-Control", "max-age=0");
				req.putHeader("Connection", "keep-alive");
				req.putHeader("Content-Type", "application/x-www-form-urlencoded");
				req.putHeader("Cookie", "JSESSIONID=00006sUwN86xGn3h2XSm-BcsaEx:19cteds09");
				req.putHeader("Host","fo.amac.org.cn");
				req.putHeader("Origin","http://fo.amac.org.cn");
				req.putHeader("Referer","http://fo.amac.org.cn/amac/allNotice.do");
				req.putHeader("Upgrade-Insecure-Requests", "1");
				req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
				
				req.putParams("currentPage", "1");
				req.putParams("mname", "listNoticeForm");
				req.putParams("GSJGMC", "");
				req.putParams("GSFWLX", ""+i);
				req.putParams("GSID", "");
				
				context.injectSeed(req);
				//------------------ 估值 ----------------
				
			}
			return Arrays.asList(context);
		}
		
	}
	
}
